﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm.NET4.Sample.CustomControl
{
    public delegate void CaptionEventHandler(object sender, int index, EventArgs e);

    public partial class CaptionBar : UserControl
    {

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        public const int WM_LBUTTONUP = 0x202;
        private Label selectedLabel;

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern IntPtr SetCapture(IntPtr h);

        public event CaptionEventHandler CaptionMouseUp;




        public CaptionBar()
        {
            InitializeComponent();
            SetStyle(ControlStyles.DoubleBuffer |
                  ControlStyles.AllPaintingInWmPaint |
                  ControlStyles.UserPaint|
                  ControlStyles.OptimizedDoubleBuffer, true);

            base.UpdateStyles();

            foreach (Label label in layoutPanel.Controls)
            {
                if (label.Name == "lblTitle")
                {
                    label.MouseDown += CaptionBar_MouseDown;
                    continue;
                }

                label.MouseDown += CaptionBar_MouseDown;
                label.MouseUp += CaptionBar_MouseUp; ;
         
                label.MouseLeave += CaptionBar_MouseLeave;
                label.MouseEnter += CaptionBar_MouseEnter;
            }
        }

        /// <summary>
        /// Occurs when a mouse button in pressed ovre the label
        /// </summary>
        private void CaptionBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && this.Parent != null)
            {
                //To provide the Dragging behavior of the Form when clicking and draging over the title bar control.
                ReleaseCapture();

                //SendMessage(this.Parent.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
                SendMessage(this.Parent.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                SendMessage(((Label)sender).Handle, WM_LBUTTONUP, 0, 0);//
            }


        }

        public void SelectFirst()
        {
            label1.ForeColor = System.Drawing.Color.White;

            label1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            CaptionMouseUp?.Invoke(label1, 1, new EventArgs());
        }

        private void CaptionBar_MouseUp(object sender, EventArgs e)
        {

            var currentLabel = (sender) as Label;

            this.selectedLabel = currentLabel;

            currentLabel.ForeColor = System.Drawing.Color.White;

            currentLabel.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            int i = 0;

            foreach (Label label in layoutPanel.Controls)
            {
                if (label.Name == "lblTitle") continue;

                i += 1;

                if (label == currentLabel)
                {
                    CaptionMouseUp?.Invoke(label, index: i, e);
                }
                else
                {
                    
                    label.ForeColor = System.Drawing.Color.Silver;

                }
            }
        }

        private void CaptionBar_MouseLeave(object sender, EventArgs e)
        {
            var label = (sender) as Label;

            if (this.selectedLabel != null && label.Name == this.selectedLabel.Name) return;

            this.Cursor = Cursors.Default;

            label.ForeColor = System.Drawing.Color.Silver;


        }

        private void CaptionBar_MouseEnter(object sender, EventArgs e)
        {
            
          
            var label = (sender) as Label;

            if (this.selectedLabel != null && label.Name == this.selectedLabel.Name) return;

            label.ForeColor = System.Drawing.Color.White;



        }

    }
}
